package com.intellagric.controller.sys_menu;


import com.intellagric.common.bean.LayuiDataGridResult;
import com.intellagric.common.bean.ResponseMessage;
import com.intellagric.interfaces.sys_menu.MenuService;
import com.intellagric.interfaces.sys_operate.OperateService;
import com.intellagric.pojo.SysMenu;
import com.intellagric.pojo.SysOperate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Auther: zhy
 * @Date: 2018/10/5
 * @Description: 菜单管理
 */
@Controller
@RequestMapping("/admin")
public class MenuController {

    @Autowired
    private MenuService menuService;
    @Autowired
    private OperateService operateService;

    /**
     * 添加菜单信息
     * @Param sysMenu
     * @return ResponseMessage
     */
    @RequestMapping("/menu/add")
    @ResponseBody
    public ResponseMessage add(SysMenu sysMenu) {
        //设置默认数据
        sysMenu.setId(UUID.randomUUID().toString().replaceAll("-",""));
        sysMenu.setDelFlag("0");
        sysMenu.setCreateDate(new Date());
        sysMenu.setUpdateDate(new Date());
        return menuService.addMenu(sysMenu);
    }


    /**
     * 删除菜单信息
     * @Param id
     * @return ResponseMessage
     */
    @RequestMapping("/menu/delete")
    @ResponseBody
    public ResponseMessage delete(String id) {
        //这里没有处理删除父类结点需要的循环删除该结点的子节点//////////////////////////////////////
        return menuService.deleteMenu(id);
    }

    /**
     * 修改菜单信息
     * @Param sysMenu
     * @return ResponseMessage
     */
    @RequestMapping("/menu/edit")
    @ResponseBody
    public ResponseMessage edit(SysMenu sysMenu) {
        return menuService.editMenu(sysMenu);
    }

    /**
     * 查询菜单信息
     * @return SysMenu
     */
    @RequestMapping("/menu/{id}")
    @ResponseBody
    public SysMenu get(@PathVariable String id) {
        return menuService.getMenuById(id);
    }

    /**
     * 异步查询菜单信息列表
     * @return SysMenu
     */
    @RequestMapping("/menu/{parentId}/sublist")
    @ResponseBody
    public LayuiDataGridResult getSubList(@PathVariable String parentId) {
        return menuService.getMenuByParentId(parentId);
    }

    /**
     * 查询菜单树信息，只返回树结点的数据
     * @return LayuiDataGridResult
     */
    @RequestMapping("/menu/treelist")
    @ResponseBody
    public List<SysMenu> getTreeList() {
        LayuiDataGridResult menu = menuService.getMenu();
        return menu.getData();
    }

    /**
     * 查询菜单树信息
     * @return LayuiDataGridResult
     */
    @RequestMapping("/menu/tree")
    @ResponseBody
    public LayuiDataGridResult getTree() {
      return  menuService.getMenu();
    }

    /**
     * 查询权限，菜单和操作
     * @return
     */

    @RequestMapping("/menu/powerTree")
    @ResponseBody
    public LayuiDataGridResult getpowerTree() {
        //这里再加入sys_operate即操作权限的菜单

        List<SysOperate> operates=   operateService.getOperateTree().getData();

        List<SysMenu> menuList = menuService.getMenu().getData();

        List data=menuList;
        data.addAll(operates);

        LayuiDataGridResult layuiDataGridResult=new LayuiDataGridResult();
        layuiDataGridResult.setData(data);
        return layuiDataGridResult;
    }



    /**
     * 查询菜单信息
     * @return LayuiDataGridResult
     */
    @RequestMapping("/menu/list")
    @ResponseBody
    public LayuiDataGridResult getList() {
        return menuService.getMenuList();
    }

}
